﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using Core;
using Data;
using Model;
using Web.Areas.sys.Controllers;

namespace Web.Areas.master
{
    public class AgencyController : Controller
    {
        //
        // GET: /master/Agent/
        public ActionResult Index() {
            var model = new {
                idField = "Agency_Id",
                urls = new {
                    query = "/api/master/agency",
                    add = "/sys/agency/edit",
                    edit = "/sys/agency/edit/",
                    remove = "/api/master/agency/edit/"
                },
                resx = new {
                    editTitle = "Edit Agency",
                    addTitle = "Add Agency",
                    removeComfirm = "Are you sure you want to delete this record?",
                    removeSuccess = "delete successfully!",
                    removeFailed = "delete failed!"
                },
                dataSource = new {
                    items = new Agency_MService().GetModels()
                },
                form = new {
                    Agency_Name = "",
                    Agency_desc = ""
                }
            };

            return View(model);
        }

        public ActionResult Edit(string id) {
            var data = new AgencyApiController().GetMaster(id);

            var model = new {
                form = data.form,
                urls = new {
                    edit = "/api/master/agency/edit"
                },
                resx = new {
                    saveSuccess = "Save Successfully!",
                    editSuccess = "Edit Successfully!"
                },
                dataSource = new { test = "test" },
                defaultForm = new Agency_M() {
                    Agency_Name = "",
                    Agency_desc = ""
                }
            };

            return View(model);
        }
    }

    public class AgencyApiController : ApiController
    {

        Agency_MService service = new Agency_MService();
        public dynamic Get(ReqWrapperQuery req) {
            var xml = @"
<SqlSetting DefaultSortName='role_code' DefaultSortOrder ='asc'>
    <Select>A.*</Select>
    <From>sys_role A</From>
    <Where>
        <Fields>
            <Field Name='Agency_Name'     Cp='like'></Field>
            <Field Name='Agency_desc '     Cp='like'> </Field>
        </Fields>
    </Where>
</SqlSetting>";
            var setting = req.ToParam(xml);
            var ret = service.GetDynamicListWithPaging(setting);
            return ret;
        }

        public dynamic GetMaster(string id) {
            var model = service.GetModel(ParamQuery.Instance().AddWhere("Agency_Id", id));
            return new {
                form = model
            };
        }

        [System.Web.Http.HttpPost]
        public int Edit(ReqWrapperInsert req) {
            var xml = @"
<SqlSetting>
    <Table>
        Agency_M
    </Table>
    <FieldInserts>
        <Field Name='Agency_Id' IsIgnore='true'></Field>
    </FieldInserts>
</SqlSetting>
";
            var pi = req.ToParam(xml);
            return service.Insert(pi);
        }

        [System.Web.Http.HttpPut]
        public int Edit(ReqWrapperUpdate req) {
            var xml = @"
<SqlSetting>
    <Table>
        Agency_M
    </Table>
    <Where>
        <Fields>
            <Field Name='Agency_Id' Cp='equal'></Field>
        </Fields>
    </Where>
</SqlSetting>
";
            var pi = req.ToParam(xml);
            return service.Update(pi);
        }

        [System.Web.Http.HttpDelete]
        public int Edit(string id) {
            return service.Update(new ParamBase().AddFieldUpdate("state", "2").AddWhere("Agency_Id", id));
        }

    }
}
